
new_class(
  'graphics.Node',
  function(x, y, scaleX, scaleY, angle, z) {
    this.x = x || 0;
    this.y = y || 0;
    this.z = z || 0;
    this.scaleX = scaleX || 1;
    this.scaleY = scaleY || 1;
    this.angle = angle || 0;
    this._children = [];
    this._animation = [];
  },
  {
    addChild: function(n) {
      // Insert at the right z-index.
      var len = this._children.length;
      n.parentNode = this;
      for(var i = 0; i < len; ++i) {
        if (this._children[i].z <= n.z) {
          this._children.splice(i, 0, n);
          return;
        }
      }
      this._children.push(n);
    },
    removeChild: function(n) {
      var i = this._children.indexOf(n);
      if (i >= 0)
        this._children.splice(i, 1);
    },
    addAnimation: function(n) {
      n.node = this;
      this._animation.push(n);
    },
    removeAnimation: function(n) {
      var i = this._animation.indexOf(n);
      if (i >= 0)
        this._animation.splice(i, 1);
    },
    transform: function(d) {
      d.translate(this.x, this.y);
      d.scale(this.scaleX, this.scaleY);
      d.rotate(this.angle);
    },
    update: function(dt) {},
    draw: function(d) {},
    _step: function(d, dt) {
      this.update(dt);

      this._animation.each(function(i,o) {
        if (o)
          o._step(d, dt);
      });

      d.save();
      this.transform(d);
      this.draw(d);
      this._children.each(function(i,o) {
        if (o)
          o._step(d, dt);
      });
      d.restore();
    },
  })